<template>
  <div>
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="商品分类">
          <el-cascader v-model="searchForm.category_id" :options="categoryList"
            :props="{ value: 'category_id', label: 'name', children: 'child' }" @change="handleChange1"></el-cascader>
        </el-form-item>
        <el-form-item label="商品名称"><el-input size="small" v-model="searchForm.keys"
            placeholder="请输入商品名称"></el-input></el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--添加等级-->
    <div class="common-level-rail">
      <el-button size="small" type="primary" @click="addClick" icon="el-icon-plus">在线云仓</el-button>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="id" label="ID" width="88px"></el-table-column>
          <el-table-column prop="product_name" label="产品" width="400px">
            <template slot-scope="scope">
              <div class="product-info">
                <div class="pic"><img v-img-url="scope.row.details.c_goods_image" alt="" /></div>
                <div class="info">
                  <div class="name">{{ scope.row.details.c_goods_name }}</div>
                  <div class="price">¥ {{ scope.row.price }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="category.name" label="分类"></el-table-column>
          <el-table-column prop="rate" label="溢价比例%"></el-table-column>
          <!-- <el-table-column prop="details.c_original_price" label="商品原价"></el-table-column> -->
          <el-table-column prop="price" label="铺货价	"></el-table-column>
          <el-table-column prop="old_price" label="进货价	"></el-table-column>
          <el-table-column prop="details.c_sale_price" label="建议售价"></el-table-column>
          <el-table-column prop="details.c_goods_stock_valid" label="库存"></el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <div class="table-btn-column">
                <el-button @click="editClick(scope.row)" type="text" size="small"
                  v-auth="'/wps/product/edit'">编辑</el-button>
                <el-button @click="detailClick(scope.row)" type="text" size="small"
                  v-auth="'/wps/index/goodsDetail'">详情</el-button>
                <el-button @click="delClick(scope.row)" type="text" size="small"
                  v-auth="'/wps/product/product_del'">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
        :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"></el-pagination>
    </div>
    <!-- 编辑 -->
    <el-dialog title="编辑云仓商品" :visible.sync="dialogFormVisible" @close="onclose">
      <el-form :model="addForm" size="small" ref="addForm" label-width="100px">
        <el-form-item label="溢价比例" :rules="[{ required: true, message: '请输入溢价比例' }]" prop="rate">
          <el-input v-model="addForm.rate" placeholder="请输入溢价比例">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="所属分类" :rules="[{ required: true, message: '请选择所属分类' }]" prop="category_id">
          <el-cascader v-model="addForm.category_id" :options="categoryList" @change="handleChange"
            :props="{ label: 'name', value: 'category_id', children: 'child' }"></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PorductApi from '@/api/product.js';
export default {
  data() {
    return {
      categoryList: [],
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      searchForm: {
        category_id: '',
        keys: '',
      },
      dialogFormVisible: false,
      addForm: {
        id: '',
        category_id: '',
        c_father_goods_id: '',
        rate: '',
      },
    };
  },
  created() {
    this.getData();
    this.getCategory();
  },

  methods: {
    delClick(row){
      let self = this;
      self.$confirm('是否确定删除该云仓商品?', '提示', {
          type: 'warning'
        })
        .then(() => {
          PorductApi.yun_goodsDel({
           id: row.id
          }).then(data => {
            self.$message({
              message: '删除成功',
              type: 'success'
            });
            self.getData();
          });
        });
    },
    handleChange1(e) {
      this.searchForm.category_id = e[e.length - 1]
    },
    detailClick(row) {
      this.$router.push({
        path: '/wps/index/goodsDetail',
        query: {
          good_id: row.c_father_goods_id,
        }
      });
    },
    edit() {
      let self = this
      self.$refs.addForm.validate(valid => {
        if (valid) {
          PorductApi.yun_product_edit(self.addForm, true).then(data => {
            self.$message({
              message: '编辑成功',
              type: 'success'
            });
            self.dialogFormVisible = false
            self.getData();
          })
        } else {
          self.$message({
            message: '请检查必填项是否填写完整',
            type: 'error'
          });
        }
      });
    },
    /*打开编辑*/
    editClick(item) {
      console.log(item)
      this.addForm.id = item.id
      this.addForm.category_id = item.category_id
      this.addForm.c_father_goods_id = item.c_father_goods_id
      this.addForm.rate = item.rate
      this.dialogFormVisible = true
    },
    /*打开添加*/
    addClick() {
      this.$router.push({
        path: '/wps/product/goodslist'
      });
    },
    /*获取列表*/
    getCategory() {
      let self = this;
      PorductApi.productList({}, true)
        .then(data => {
          self.categoryList = data.data.category;
        })
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.loading = true;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      Params.type = self.activeName;
      self.loading = true;
      PorductApi.yun_product_list(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.data;
          self.totalDataNumber = data.data.total;
          // self.tableData.forEach(item => {
          //   item.details.c_banner_images = item.details.c_banner_images.split(';')
          // })
        })
        .catch(error => {
          self.loading = false;
        });
    },

  }

};
</script>

<style></style>
